#include "Quene.h"

void QueueInit(Queue* q)
{
	q->phead = NULL;
	q->ptail = NULL;
	q->size = 0;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	if (q->phead == NULL)
	{
		QNode* newnode = (QNode*)malloc(sizeof(QNode));
		newnode->_data = data;
		newnode->_next = NULL;
		q->phead = newnode;
		q->ptail = newnode;
		q->size++;
	}
	else
	{
		QNode* newnode = (QNode*)malloc(sizeof(QNode));
		newnode->_data = data;
		newnode->_next = NULL;
		q->ptail->_next = newnode;
		q->ptail = newnode;
		q->size++;
	}
}


void QueuePop(Queue* q)
{
	assert(q);
	assert(q->phead);
	QNode* next = q->phead->_next;
	free(q->phead);
	q->phead = next;
	q->size--;
}



QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->phead);
	return q->phead->_data;
}


QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->phead);
	return q->ptail->_data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->size;
}

void QueueDestroy(Queue* q)
{
	QNode* cur = q->phead;
	while (q->phead != NULL)
	{
		QNode* cur = q->phead->_next;
		free(q->phead);
		q->phead = cur;
	}
	free(q->phead);
}